//
import React from 'react';
import { connect } from 'dva';
import {
  ModalC,
  PrimaryC,
  TableC,
  PrimaryGhostC,
  InputNumberC,
  SecuritiesCodeC,
  codeType,
  TradingDirectC,
  TradingTypeC,
  InputC,
  SingleDateC,
  SelectC,
  NoQueryC,
  SecondHeaderC,
  TradingMarketC,
} from '../../../comps';
import { DATA_ENTRY } from '../../../comps/form/formPub';
import style from './index';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { message, Col, Row, Spin } from 'antd';
import MyCheckBox from './myCheckbox';
const FormItem = Form.Item;
@Form.create()
class SettingC extends React.Component {
  static defaultProps = {
    onRef: () => {},
  };
  state = {
    partConfig: false,
    rateConfig: false,
    ggConfig: false,
  };
  componentDidMount() {}
  UNSAFE_componentWillReceiveProps(nextProps) {
    if (nextProps.id != this.props.id) {
      this.props.dispatch({
        type: 'initialSplitM/getConfig',
        payload: {
          id: nextProps.id,
        },
        callback: (res) => {
          if (res) {
            if (res.code == 0) {
              let info = res.data || {};
              this.setState({
                partConfig: info.partConfig !== undefined ? (info.partConfig === '1' ? true : false) : true,
                rateConfig: info.rateConfig !== undefined ? (info.rateConfig === '1' ? true : false) : true,
                ggConfig: info.ggConfig !== undefined ? (info.ggConfig === '1' ? true : false) : true,
              });
            } else {
              message.error(res.msg || '查询配置信息失败!');
            }
          } else {
            message.error('查询配置信息失败!');
          }
        },
      });
    }
  }
  //点击拆分配置的确定按钮进行保存 查询配置信息
  saveConfig = (type, e) => {
    const {
      form: { validateFields },
    } = this.props;
    this.setState(
      {
        [type]: e.target.checked,
      },
      () => {
        let values = {};
        let arr = ['rateConfig', 'partConfig', 'ggConfig'];
        arr.map((item) => {
          values[item] = this.state[item] === true ? '1' : '0';
        });
        this.props.dispatch({
          type: 'initialSplitM/saveConfig',
          payload: {
            id: this.props.id,
            ...values,
          },
          callback: (res) => {
            if (res) {
              if (res.code == 0) {
                // message.success('拆分配置成功!')
              } else {
                message.error(res.msg || '拆分配置失败!');
              }
            } else {
              message.error('拆分配置失败!');
            }
          },
        });
      },
    );
  };
  render() {
    const { loading } = this.props;
    return (
      <div>
        <Spin spinning={loading.effects['initialSplitM/saveConfig'] == true}>
          <div style={{ display: 'flex' }}>
            <div style={{ paddingRight: '15px' }}>
              <MyCheckBox value={this.state.partConfig} onChange={(e) => this.saveConfig('partConfig', e)} text={'集中度限制(单一资产市值/产品净资产<10%)'} />
            </div>
            <div style={{ paddingRight: '15px' }}>
              <MyCheckBox value={this.state.rateConfig} onChange={(e) => this.saveConfig('rateConfig', e)} text={'收益率限制(资产收益率 > 资金成本)'} />
            </div>
            <div>
              <MyCheckBox value={this.state.ggConfig} onChange={(e) => this.saveConfig('ggConfig', e)} text={'杠杆率限制(总资产/净资产 < 140%)'} />
            </div>
          </div>
        </Spin>
      </div>
    );
  }
}
export default connect(({ initialSplitM, userM: user, loading }) => ({
  initialSplitM,
  user,
  loading,
}))(SettingC);
